#include "cocos2d.h"
#include "Component/CollisionTree.h"

USING_NS_CC;
USING_NS_CC_EXT;

CollisionTree::CollisionTree(Monkey *monkey,Cloud *cloud)
{
    setName("Move");
    curAction = NULL;
	this->mMonkey = monkey;
	this->mCloud = cloud;
	this->mMovedLeft = 0;
	this->mMovedRight = 0;
}

void CollisionTree::fMoveUp()
{
	if(curAction)
	{
		actionStop();
	}

	visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCMoveBy* actionMove = CCMoveBy::create(kTreeMoveUpSpeed ,ccp(0,visibleSize.height + parent->getContentSize().height/2));
	curAction = actionMove;
	parent->runAction(CCSequence::createWithTwoActions(actionMove , CCCallFuncO::create(actionMove , callfuncO_selector(CollisionTree::fMoveUpCallback) , (CCObject *) parent)));
}

void CollisionTree::fMoveLeft()
{
	if(curAction)
	{
		actionStop();
	}
	//CCLog("Move left called");
	visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	float xDistance = std::fabs(visibleSize.width/2 - parent->getPositionX());

	CCMoveBy* actionMove = CCMoveBy::create(1.5f ,ccp(-xDistance,0));
	curAction = actionMove;
	parent->runAction(CCSequence::createWithTwoActions(actionMove , CCCallFuncO::create(actionMove , callfuncO_selector(CollisionTree::fJumpCallback) , (CCObject *) parent)));
	//parent->runAction(CCSequence::create(actionMove,NULL));
}

void CollisionTree::fMoveRight()
{
	if(curAction)
	{
		actionStop();
	}
	//CCLog("Move right called");
	
	visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	float xDistance = std::fabs(visibleSize.width/2 - parent->getPositionX());

	CCMoveBy* actionMove = CCMoveBy::create(1.5f ,ccp(xDistance,0));
	curAction = actionMove;
	parent->runAction(CCSequence::createWithTwoActions(actionMove , CCCallFuncO::create(actionMove , callfuncO_selector(CollisionTree::fJumpCallback) , (CCObject *) parent)));
	//parent->runAction(CCSequence::create(actionMove,NULL));
}

void CollisionTree::fMoveUpCallback(CCObject * object){
	//CCLOG("Move Up CALL back");
}

void CollisionTree::fJumpCallback(CCObject * object){
	//CCLOG("Jump CALL back");
	mMovedRight = 0;
	mMovedLeft = 0;
}

// Xu ly va cham
void CollisionTree::update(float delta)
{
	parent = (Tree*)getOwner();

	//mMonkey->draw();
	if (mMonkey->fGetStatus() == isDie) return;


	if(mMonkey->fGetStatus() == isJump)
	{
		//fMoveUp();
	}
	if(mMonkey->fGetStatus() == isJumpLeft && mMovedRight == 0)
	{
		// Cay lien tuc di chuyen le tren
		//fMoveUp();
		// Neu con khi di chuyen sang trai thi cay chay sang phai
		fMoveRight();

		mCloud->fMoveRight();

		mMovedRight = 1;

		//CCLog("Tree Moved To The Right");

	}

	if(mMonkey->fGetStatus() == isJumpRight && mMovedLeft == 0)
	{
		// Cay lien tuc di chuyen le tren
		//fMoveUp();
		// Neu con khi di chuyen sang phai thi cay chay sang trai
		fMoveLeft();

		mCloud->fMoveLeft();

		mMovedLeft = 1;

		//CCLog("Tree Moved To The Left");
	}


	CCRect nBoundingTree = parent->fGetSprite()->boundingBox();
	CCRect nRectBodyMonkey = mMonkey->fGetArmature()->getBone("Layer6")->getDisplayRenderNode()->boundingBox();
	if (fIsCollisionOfBody(nBoundingTree,nRectBodyMonkey))
    {
        CCLog("Da cham voi cay roi !!!");
		parent->removeComponent("MoveDown");
		parent->removeComponent("Move");
		mMonkey->fSetStatus(Idle);
		//mMonkey->fSetStatus(isDie);
    }
}

bool CollisionTree::fIsCollisionOfBody(CCRect sp1 , CCRect sp2)
{
  CCRect rectSp1 = sp1;
  CCRect rectSp2 = sp2;
  float minX1, minY1, maxX1, maxY1, minX2, minY2, maxX2, maxY2, tem;
  float nScaleMonkey = mMonkey->fGetArmature()->getScaleX();
  CCPoint nPointMonkey = mMonkey->getPosition();
  CCPoint nPointTree = parent->getPosition();



  //set tham so object 1
  minX1 = rectSp1.getMinX() + nPointTree.x;
  minY1 = rectSp1.getMinY() + nPointTree.y;
  maxX1 = rectSp1.getMaxX() + nPointTree.x;
  maxY1 = rectSp1.getMaxY() + nPointTree.y;

  //set tham so object 2
  minX2 = rectSp2.getMinX() * nScaleMonkey + nPointMonkey.x;
  minY2 = rectSp2.getMinY() + nPointMonkey.y;
  maxX2 = rectSp2.getMaxX() * nScaleMonkey + nPointMonkey.x;
  maxY2 = rectSp2.getMaxY() + nPointMonkey.y;
  if(nScaleMonkey < 0) 
	{
		tem = minX2;
		minX2 = maxX2;
		maxX2 = tem;
	}


  return !(maxX1 < minX2 ||
   maxX2 < minX1 ||
   maxY1 < minY2 ||
   maxY2 < minY1);
}

bool CollisionTree::fIsCollision(CCNode * sp1 , CCArmature * sp2 , int minSize)
{

	CCSize s = sp1->getContentSize();
    CCPoint p = sp1->getPosition();
    CCPoint ap = sp1->getAnchorPoint();

	CCRect rectSp1 = CCRectMake(
                            p.x - ap.x * s.width ,
                             p.y - ap.y * s.height,
                             s.width, s.height);

	 CCRect rectSp2 = sp2->boundingBox();
	 float minX1, minY1, maxX1, maxY1, minX2, minY2, maxX2, maxY2;

	 //set tham so object 1
	 minX1 = rectSp1.getMinX();
	 minY1 = rectSp1.getMinY();
	 maxX1 = rectSp1.getMaxX();
	 maxY1 = rectSp1.getMaxY();

	 //ccDrawRect(ccp(minX1-20,maxY1),ccp(maxX1+20,minY1));

	 //set tham so object 2
	 minX2 = rectSp2.getMinX();
	 minY2 = rectSp2.getMinY();
	 maxX2 = rectSp2.getMaxX();
	 maxY2 = rectSp2.getMaxY();

	//ccDrawRect(ccp(minX2,maxY2),ccp(maxX2,minY2));

	 return !(maxX1 < minX2 ||
	  maxX2 < minX1 ||
	  maxY1 < minY2 ||
	  maxY2 < minY1);
}

void CollisionTree::actionStop()
{
    if (curAction != NULL)
    {
        parent->stopAction(curAction);
        curAction = NULL;
    }
}

